package com.nebula.service.impl;

import com.nebula.dao.impl.BenefitsDaoImpl;
import com.nebula.common.DataGridFooter;
import com.nebula.common.PageResult;
import com.nebula.dao.IBenefitsDao;
import com.nebula.entity.Benefits;
import com.nebula.service.IBenefitsService;
import com.nebula.util.DateTimeUtil;
import com.nebula.vo.BenefitsVo;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author CcccX
 * @since 2020-07-05 20:22
 */
public class BenefitsServiceImpl implements IBenefitsService {
    private IBenefitsDao iBenefitsDao = new BenefitsDaoImpl();

    @Override
    public PageResult<BenefitsVo> findByUserIdAndPage(String id, int page, int size, int... year) {
        String conditions = " where user_id = '" + id + "'";
        if (year[0] != -1){
            conditions += " and issue_year = " + year[0];
        }
        List<Benefits> benefitsList = iBenefitsDao.findByPage(page, size, conditions);
        List<BenefitsVo> benefitsVos = new ArrayList<>();
        BigDecimal bd = new BigDecimal(0);
        for (Benefits benefits : benefitsList) {
            BenefitsVo benefitsVo = assembleBenefitsVo(benefits);
            bd = bd.add(benefits.getSalary());
            benefitsVos.add(benefitsVo);
        }
        PageResult<BenefitsVo> pageResult = new PageResult<>();
        pageResult.setCurrPage(page);
        pageResult.setList(benefitsVos);

        Integer totalCount = iBenefitsDao.selectCount("benefits", conditions);
        Double dc = Math.ceil(totalCount.doubleValue() / size);
        pageResult.setTotalCount(totalCount);
        pageResult.setPageSize(dc.intValue());
        NumberFormat nf = NumberFormat.getCurrencyInstance();
        pageResult.setCurrTotalSalary(nf.format(bd));

        return pageResult;
    }

    @Override
    public DataGridFooter findTotalBenefits(String userId, int... year) {
        String conditions = "";
        if (year[0] != -1){
            conditions += " and issue_year = " + year[0];
        }
        BigDecimal totalSalary = iBenefitsDao.getTotalSalary(userId, conditions);
        NumberFormat format = NumberFormat.getCurrencyInstance();
        DataGridFooter dataGridFooter = new DataGridFooter();
        if (totalSalary != null) {
            String s = format.format(totalSalary);
            dataGridFooter.setTotalSalary(s);
        } else {
            dataGridFooter.setTotalSalary(format.format(0));
        }

        return dataGridFooter;
    }

    @Override
    public void save(Benefits benefits) {
        iBenefitsDao.save(benefits);
    }

    @Override
    public void update(Benefits benefits) {
        iBenefitsDao.update(benefits);
    }

    @Override
    public BenefitsVo findById(int id) {
        Benefits benefits = iBenefitsDao.findById(id);
        return assembleBenefitsVo(benefits);
    }

    @Override
    public void delete(int id) {
        iBenefitsDao.delete(id);
    }

    private BenefitsVo assembleBenefitsVo(Benefits benefits) {
        BenefitsVo benefitsVo = new BenefitsVo();
        benefitsVo.setId(benefits.getId());
        benefitsVo.setIssueDate(DateTimeUtil.dateToStr(benefits.getIssueDate(), "yyyy-MM-dd"));
        benefitsVo.setCreatedTime(DateTimeUtil.dateToStr(benefits.getCreatedTime()));
        benefitsVo.setUpdatedTime(DateTimeUtil.dateToStr(benefits.getUpdatedTime()));
        benefitsVo.setRemark(benefits.getRemark());
        benefitsVo.setUser(benefits.getUserId());
        benefitsVo.setYear(benefits.getIssueYear());
        benefitsVo.setMonth(benefits.getIssueMonth());
        benefitsVo.setSalaryNum(benefits.getSalary());

        NumberFormat nf = NumberFormat.getCurrencyInstance();
        if (benefits.getSalary() != null) {
            benefitsVo.setSalary(nf.format(benefits.getSalary()));
        } else {
            benefitsVo.setSalary(nf.format(0));
        }

        return benefitsVo;
    }
}
